<template>
  <view class="pages">
    <!-- v-if="isLogin" -->
    <view
      v-if="isLogin"
      class="wx-login"
    >
      <view
        v-if="false"
        class="wx-login-title"
        @click="handleWechatRegister()"
      >
        <button>注册</button>
      </view>
      <view
        class="wx-login-title"
        @click="handleWechatLogin()"
      >
        <button
          open-type="getPhoneNumber"
          @getphonenumber="handleGetPhone"
        >
          微信登录
        </button>
      </view>
    </view>
    <view>
      <Message />
    </view>
    <!-- <view
      class=""
      v-if="tabsSwitch == 1"
    >
      <WorkBenches />
    </view>
    <view v-if="tabsSwitch == 2">
      <MyFile />
    </view>
    <view v-if="tabsSwitch == 3">
      <AddressBook />
    </view>
    <tabBar @tabsBottomSwitch="handleTabsSwitch" /> -->
  </view>
</template>

<script setup>
import { onBeforeMount, onMounted, ref, watch } from "vue";
import Taro, { navigateTo, useLoad, showToast } from "@tarojs/taro";
import tabBar from "@/components/tabBar/index.vue";
import { userInfoStore } from "@/stores/userInfoStore";
import Message from "../../packageC/pages/message/index.vue";
import WorkBenches from "../../packageA/pages/workbenches/index.vue";
// import MyFile from "../../packageA/pages/my/myFile/index.vue";
// import AddressBook from "../../packageB/pages/addressBook/index";
const store = userInfoStore();
import { getLoginSession, getLoginPhone, getUserInfo } from "@/api/index";
import { wechatLogin } from "@/libs/wechat/runtime/index";
const isLogin = ref(!Taro.getStorageSync("token"));
const userPhone = ref("");
const tabsSwitch = ref(0);
const lists = ref([
  "https://storage.360buyimg.com/jdc-article/NutUItaro34.jpg",
  "https://storage.360buyimg.com/jdc-article/NutUItaro2.jpg",
  "https://storage.360buyimg.com/jdc-article/welcomenutui.jpg",
  "https://storage.360buyimg.com/jdc-article/fristfabu.jpg",
]);
//页面加载
watch(
  () => userInfoStore().tabsBottomSwitch,
  (newVal, oldVal) => {
    console.log(userInfoStore().tabsBottomSwitch, "userInfoStore().tabsBottomSwitch");
    // tabsSwitch.value = userInfoStore().tabsBottomSwitch;
  },
);
onMounted(() => {
  if (store.punchCards.checkInTime == null && store.punchCards.checkOutTime == null) {
    navigateTo({
      url: "/packageC/pages/checkIn/index",
    });
  }
});
// watch(
//   () => store.punchCards,
//   (newVal, oldVal) => {
//     if (store.punchCards.checkInTime == null && store.punchCards.checkOutTime == null) {
//       navigateTo({
//         url: "/packageC/pages/checkIn/index",
//       });
//     }
//   },
// );
const handleTabsSwitch = val => {
  tabsSwitch.value = val;
  console.log(val, "val98989898");
};
// 获取手机号
const handleGetPhone = e => {
  console.log(e, "e");
  if (e.detail.errMsg === "getPhoneNumber:ok") {
    getLoginPhone({
      sessionKey: Taro.getStorageSync("sessionKey"),
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
    })
      .then(res => {
        userPhone.value = res.data.phoneNumber;
        Taro.setStorageSync("token", res.data.access_token);
        Taro.setStorageSync("clientid", res.data.client_id);
        // Taro.setStorageSync("userInfo", res.data);
        store.getUserInfo(); //user
        showToast({ title: "登录成功", icon: "none" });
        // 登录成功后刷新页面
        if (res.data.code == 401 && res.data.msg == "认证失败，无法访问系统资源") {
          Taro.redirectTo({ url: "/pages/login/index" });
        } else {
          Taro.reLaunch({
            url: "/pages/index/index",
          });
        }
      })
      .catch(err => {
        showToast({ title: err.msg, icon: "none" });
      });
  }
};
// 获取用户信息
// 注册
// const handleWechatRegister = async () => {
//   getRegister({}).then(res => {
//     console.log("注册成功:", res);
//   });
// };
//登录
const handleWechatLogin = async () => {
  await wechatLogin().then(res => {
    getLoginSession({
      code: res.code,
    }).then(res => {
      console.log("登录成功66:", res.data);
      Taro.setStorageSync("sessionKey", res.data.sessionKey);
    });
  });
};
</script>
<style lang="less">
@import "./index.less";
</style>
